/**
 * @fileoverview OO interface to handle cookies.
 * Taken from {@link http://www.quirksmode.org/js/cookies.html}.
 * Regarding licensing of this code the author states:
 *
 * "You may copy, tweak, rewrite, sell or lease any code example on
 * this site, with one single exception."
 *
 * @author 2003-2006 Peter-Paul Koch
 * @author Stefan Strigler
 */


/*exported JSJaCCookieException, JSJaCCookie */

/**
 * Some exception denoted to dealing with cookies
 * @constructor
 * @param {String} msg The message to pass to the exception
 */
class JSJaCCookieException {
  name:string="CookieException"
  constructor(public message:string) {

  }
}

/**
 * Creates a new Cookie
 * @class Class representing browser cookies for storing small amounts of data
 * @constructor
 * @param {String} name   The name of the value to store
 * @param {String} value  The value to store
 * @param {int}    secs   Number of seconds until cookie expires (may be empty)
 * @param {String} domain The domain for the cookie
 * @param {String} path   The path of cookie
 */
export  class JSJaCCookie {
  /**
   * 
   * @param name  This cookie's name
   * @param value This cookie's value
   * @param secs  Time in seconds when cookie expires (thus being delete by
   * browser). A value of -1 denotes a session cookie which means that
   * stored data gets lost when browser is being closed.
   * @param domain  The cookie's domain
   * @param path  The cookie's path
   */
  constructor(public name:string,public value:string,public secs?:number,
    public domain?:string,public path?:string) {
    
  }
  /**
   * Stores this cookie
   */
  write():void {
    let expires:string;
    if (this.secs) {
      let date:Date = new Date();
      date.setTime(date.getTime()+(this.secs*1000));
      expires = "; expires="+date.toUTCString();
    } else
      expires = "";
    let domain:string = this.domain?"; domain="+this.domain:"";
    let path:string = this.path?"; path="+this.path:"; path=/";
    document.cookie = this.name+"="+JSJaCCookie._escape(this.value)+ expires+ domain+ path;
  };
  /**
   * Deletes this cookie
   */
  erase() {
    let c:JSJaCCookie = new JSJaCCookie(this.getName(),"",-1);
    c.write();
  };
  /**
   * Gets the name of this cookie
   * @return The name
   * @type String
   */
  getName():string {
    return this.name;
  };
  /**
   * Sets the name of this cookie
   * @param {String} name The name for this cookie
   * @return This cookie
   * @type Cookie
   */
  setName(name):JSJaCCookie {
    this.name = name;
    return this;
  };
  /**
   * Gets the value of this cookie
   * @return The value
   * @type String
   */
  getValue():string {
    return this.value;
  };

  /**
   * Sets the value of this cookie
   * @param {String} value The value for this cookie
   * @return This cookie
   * @type Cookie
   */
  setValue(value:string):JSJaCCookie {
    this.value = value;
    return this;
  };

  /**
   * Sets the domain of this cookie
   * @param {String} domain The value for the domain of the cookie
   * @return This cookie
   * @type Cookie
   */
  setDomain(domain:string):JSJaCCookie {
    this.domain = domain;
    return this;
  };

  /**
   * Sets the path of this cookie
   * @param {String} path The value of the path of the cookie
   * @return This cookie
   * @type Cookie
   */
  setPath(path:string):JSJaCCookie {
    this.path = path;
    return this;
  };
  /**
   * Reads the value for given <code>name</code> from cookies and return new
   * <code>Cookie</code> object
   * @param {String} name The name of the cookie to read
   * @return A cookie object of the given name
   * @type Cookie
   * @throws CookieException when cookie with given name could not be found
   */
  static read(name:string):JSJaCCookie {
    let nameEQ:string = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) === 0)
        return new JSJaCCookie( name,
          JSJaCCookie._unescape(c.substring(nameEQ.length,c.length)));
    }
    throw new JSJaCCookieException("Cookie not found");
  };
/**
 * Reads the value for given <code>name</code> from cookies and returns
 * its valued new
 * @param {String} name The name of the cookie to read
 * @return The value of the cookie read
 * @type String
 * @throws CookieException when cookie with given name could not be found
 */
static get(name:string):string{
  return JSJaCCookie.read(name).getValue();
};

/**
 * Deletes cookie with given <code>name</code>
 * @param {String} name The name of the cookie to delete
 * @throws CookieException when cookie with given name could not be found
 */
static remove(name:string):void {
  JSJaCCookie.read(name).erase();
};

/**
 * @private
 */
private static _escape(str:string):string {
  return str.replace(/;/g, "%3AB");
};

/**
 * @private
 */
private static _unescape(str:string):string {
  return str.replace(/%3AB/g, ";");
};

}





